package wsf.akkascala

import java.util.concurrent.TimeUnit

import akka.actor.{ActorSystem, Kill, PoisonPill, Props}
import akka.pattern.Patterns
import akka.util.Timeout

import scala.concurrent.Await

/**
 * Created by sjzx on 2016/5/13.
 */
object Main {
  def main (args: Array[String]) {
    val system = ActorSystem("akkatest")//create Master actor
    val service = system.actorOf(Props(classOf[wsf.akkascala.MasterActor]), "akkaservice")  //create master actor,need not start.  it's the parent actor,other actor create by this actor
    println(service.path) //the actor of master's path  akka://akkatest/user/akkaservice
    val list = List("today is a good day!", "My name is wei shou feng!", "what is your name","我是 个中国人", "你是什么人")
    list foreach (service ! _)//send data to Master which to be deal
    Thread.sleep(500) //wait work gone
//    service a "res"//get result
    val timeout =  Timeout(1, TimeUnit.SECONDS)
    val pattern = Patterns.ask(service, "res", timeout)  //使用等待的方式发送消息并得到结果
    val res = Await.result(pattern, timeout.duration)
    println(res)
    service ! "become"
    Thread.sleep(500)
//    service ! PoisonPill  //send PoisonPill to stop the actor!
//    service ! Kill//send Kill to stop the actor!
    system.terminate//shut down
  }
}
